package me.sdevil507.biz.common.sysdict.repository;

import me.sdevil507.biz.common.sysdict.po.SysDict;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: tips_back_end
 * @description: 字典dao
 * @author: kzl
 * @create: 2020-06-29 11:24
 **/
public interface SysDictRepository extends JpaRepository<SysDict, Long>, JpaSpecificationExecutor<SysDict>, QuerydslPredicateExecutor<SysDict> {

    /**
     * 查询排序
     *
     * @param id 字典id
     */
    @Query(nativeQuery = true, value ="SELECT * FROM `biz_sysdict` bs where type_key=?1 and is_del = 0 order by bs.d_value+0")
    List<SysDict> getAllSysDictData(String typeKey);
    /**
     * 逻辑删除
     *
     * @param id 字典id
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update SysDict a set a.isDel=true where a.id=:id")
    void logicDelete(@Param("id") Long id);


    /**
     * 逻辑删除
     *
     * @param parentId 字典父级id
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true)
    @Query(value = "update SysDict  a set a.isDel=true where a.parentId=:parentId")
    void logicDeleteByParentId(@Param("parentId") Long parentId);
}
